/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.hohai.river.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hohai.river.entity.Score;
import com.hohai.river.service.ScoreService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 分数表
 *
 * @author pig code generator
 * @date 2022-09-17 13:11:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/score")
@Tag(name = "分数表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ScoreController {

	private final ScoreService scoreService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param score 分数表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('river_score_get')")
	public R getScorePage(Page page, Score score) {
		return R.ok(scoreService.page(page, Wrappers.query(score)));
	}

	/**
	 * 通过id查询分数表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('river_score_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(scoreService.getById(id));
	}

	/**
	 * 新增分数表
	 * @param score 分数表
	 * @return R
	 */
	@Operation(summary = "新增分数表", description = "新增分数表")
	@SysLog("新增分数表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('river_score_add')")
	public R save(@RequestBody Score score) {
		return R.ok(scoreService.save(score));
	}

	/**
	 * 修改分数表
	 * @param score 分数表
	 * @return R
	 */
	@Operation(summary = "修改分数表", description = "修改分数表")
	@SysLog("修改分数表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('river_score_edit')")
	public R updateById(@RequestBody Score score) {
		return R.ok(scoreService.updateById(score));
	}

	/**
	 * 通过id删除分数表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除分数表", description = "通过id删除分数表")
	@SysLog("通过id删除分数表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('river_score_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(scoreService.removeById(id));
	}

	/**
	 * 查询分数
	 * @return R
	 */
	@Operation(summary = "查询分数", description = "查询分数")
	@SysLog("查询分数")
	@GetMapping("/num")
	public R getScoreNum(@RequestParam("riverId") Long riverId, @RequestParam("indexId") Long indexId) {
		Score score = scoreService.getScore(riverId, indexId);
		return R.ok(score == null ? null : score.getSystemScore());
	}

	/**
	 * 统计指标评分总值
	 */
	@Operation(summary = "统计指标评分总值", description = "统计指标评分总值")
	@SysLog("统计指标评分总值")
	@GetMapping("/calculate")
	public R calculateTotalScore(@RequestParam("riverId") Long riverId) {
		return R.ok(scoreService.calculateTotalScore(riverId));
	}

	/**
	 * 获取指标最大分值
	 */
	@Operation(summary = "获取指标最大分值", description = "获取指标最大分值")
	@SysLog("获取指标最大分值")
	@GetMapping("/max")
	public R getMaxScore(@RequestParam("level") Integer level) {
		return R.ok(scoreService.getMaxScore(level));
	}

}
